package cn.tedu.boot5.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
public class UploadController {
    @RequestMapping("/upload")
    public String upload(MultipartFile picFile) throws IOException {
        //得到源文件名
        String fileName = picFile.getOriginalFilename();
        System.out.println(fileName);
        //得到文件名后缀,grassland.jpg    从.开始 截取到最后
        String suffix = fileName.substring(fileName.lastIndexOf("."));
        fileName = UUID.randomUUID()+suffix;
        System.out.println(fileName);
        String dirPath = "D:/XCTF";
        File dirFile = new File(dirPath);
        if (!dirFile.exists()){
            System.out.println("创建文件夹");
            dirFile.mkdirs();
        }
        //D:/XCTF/******.jpg
        String filePath = dirPath+"/"+fileName;
        picFile.transferTo(new File(filePath));
        //把文件名响应给客户端
        return "/"+fileName;
    }

}
